
package com.afd.ozblip.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.afd.ozblip.adapter.BusinessListAdapter;
import com.afd.ozblip.app.CommonInfo;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignBusinessListFragment extends Fragment {

    /****************** 视图变量 **************************/
    private LinearLayout activityLayout;
    private EditText searchEt;
    private ListView signListView;
    /****************** 其它变量 **************************/
    private IndexActivity mActivity;
    private List<BusinessEntity> signBusinessList;
    private BusinessListAdapter signBusinessListAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mActivity = (IndexActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity 必须是 "
                    + IndexActivity.class.getName());
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取参数
        getBundle();

    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取列表数据
        requestTopListData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.sign_business_list, container, false);
        // 寻找视图
        findViews(rootView);
        // 设置监听
        setListeners();
        return rootView;
    }

    /**
     * 寻找视图
     */
    public void findViews(View rootView) {
        this.activityLayout = (LinearLayout) rootView.findViewById(R.id.ll_sign_activity);
        this.signListView = (ListView) rootView.findViewById(R.id.lv_sign_businessList);
    }

    /**
     * 设置监听
     */
    public void setListeners() {

        View.OnClickListener signClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_sign_activity:
                        Intent settingIntent = new Intent(mActivity,
                                ActivityListActivity.class);
                        startActivity(settingIntent);
                        break;

                    default:
                        break;
                }
            }
        };
        this.activityLayout.setOnClickListener(signClick);
        this.signListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> itemView, View arg1,
                    int parameInt, long arg3) {
                Intent businessIntent = new Intent(mActivity,
                        BusinessInfoActivity.class);
                BusinessEntity business = SignBusinessListFragment.this.signBusinessList
                        .get(parameInt);
                businessIntent.putExtra("businessInfo", business);
                startActivity(businessIntent);
            }
        });
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        // Intent intent = getIntent();
    }

    /**
     * TODO 获取签到商家列表数据
     */
    private void requestTopListData() {

        AsyncLoadData ald = new AsyncLoadData(mActivity, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
                        nearbyBusinessParamMap.put("sellerScope", "1000000000");
                        if (CommonInfo.commonLocation != null) {
                            nearbyBusinessParamMap.put("userLongitude",
                                    CommonInfo.commonLocation.getLatitude());
                            nearbyBusinessParamMap.put("userDimension",
                                    CommonInfo.commonLocation.getLongitude());
                        }
                        nearbyBusinessParamMap.put("pageCnt", "1");
                        nearbyBusinessParamMap.put("pageRow", "20");
                        nearbyBusinessParamMap.put("sort", "0");
                        nearbyBusinessParamMap.put("language", CommonInfo.commonLanguage);
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                mActivity,
                                Constants.SELLER_LIST_API,
                                nearbyBusinessParamMap);
                        if (commentItemList != null) {
                            Map<String, Object> resultMap = new ParseJson()
                                    .parseBusinessListJson(commentItemList);
                            signBusinessList = (List<BusinessEntity>) resultMap.get("data");
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        setTopListAdapter();
                    }

                });
        ald.execute(0);
    }

    /**
     * 调用评论列表适配器
     */
    public void setTopListAdapter() {
        if (CollectionUtil.UnNullAndZero(signBusinessList)) {
            this.signBusinessListAdapter = new BusinessListAdapter(mActivity);
            this.signBusinessListAdapter.addItems(signBusinessList);
            this.signListView.setAdapter(this.signBusinessListAdapter);
        }
    }
}
